<template>
  <div class="categories">
    <el-button type="success" plain class="addCategory" @click="showAddCategory">添加分类</el-button>
    <!-- 表格 -->
    <el-table :data="form"
    style="margin-top:10px"
     row-key="cat_id"  v-loading="isLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(255, 255, 255, 0.9)">

      <el-table-column label="分类名称" prop="cat_name"></el-table-column>
      <el-table-column label="是否有效">
        <template v-slot="{row}">
          {{row.cat_deleted ? '否' : '是'}}
        </template>
      </el-table-column>
      <el-table-column label="层级" prop="cat_level"></el-table-column>
      <el-table-column label="操作">
        <el-button type="primary" plain size="small" icon="el-icon-edit"></el-button>
        <el-button type="danger" plain size="small" icon="el-icon-delete"></el-button>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="dialogVisible"
      width="40%"
      @close="resetDialog"
      >
        <template>
          <el-form :rules="rules" label-width="80px" :model="cateForm" status-icon ref="cateForm">
            <el-form-item label="分类名称"  prop="cat_name">
              <el-input placeholder="请输入分类名称" v-model="cateForm.cat_name" ></el-input>
            </el-form-item>
             <el-form-item label="父级名称" prop="cat_pid">
                 <el-cascader
                  clearable
                  v-model="cateForm.cat_pid"
                  :options="options"
                  :props="props"
                  >
                </el-cascader>
            </el-form-item>
          </el-form>
        </template>
      <template v-slot:footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCategory">确 定</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
export default {
  created () {
    this.getCategoryList()
  },
  data () {
    return {
      form: [],
      pagenum: 1,
      pagesize: 5,
      total: 0,
      isLoading: false,
      dialogVisible: false,
      options: [],
      props: {
        value: 'cat_id',
        label: 'cat_name',
        checkStrictly: true
      },
      cateForm: {
        cat_pid: '',
        cat_name: '',
        cat_level: ''
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    async getCategoryList () {
      this.isLoading = true
      const { meta, data } = await this.$axios.get('categories?type=3', {
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      console.log(data)

      if (meta.status === 200) {
        this.form = data.result
        this.total = data.total
      }
      this.isLoading = false
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.pagenum = 1
      this.getCategoryList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.pagenum = val
      this.getCategoryList()
    },
    async showAddCategory () {
      this.dialogVisible = true
      const res = await this.$axios.get('categories?type=2')
      console.log(res)
      this.options = res.data
    },
    async addCategory () {
      try {
        await this.$refs.cateForm.validate()
        const pid = this.cateForm.cat_pid
        const res = await this.$axios.post('categories', {
          cat_pid: pid,
          cat_name: this.cateForm.cat_name,
          cat_level: pid.length
        })
        console.log(res)
      } catch (e) {
        console.log(e)
      }
    },
    resetDialog () {
      this.$refs.cateForm.resetFields()
    }
  }

}
</script>

<style lang="less" scoped>
  /deep/.addCategory {
    margin-bottom: 10px;
  }
</style>
